package com.jamal2362.apktool.task;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.widget.ImageView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import com.jamal2362.apktool.R;
import com.jamal2362.apktool.util.Apkinfo;
import com.jamal2362.apktool.util.IconFactory;
import com.jamal2362.apktool.util.IconGenerate;
import com.jamal2362.apktool.util.IconTypes;
import com.jamal2362.apktool.util.Settings;

public class IconTask extends AsyncTask<File, Integer, Bitmap> {
	private ImageView icon;
	private Context context;
	private IconFactory icFactory;
	private String icTag;

	public IconTask(Context ctx, ImageView ic) {
		context = ctx;
		icon = ic;
		icTag = icon.getTag().toString();
		icFactory = new IconFactory(context);
		icFactory.round(Settings.roundIcons);
		// icon.setImageBitmap(icFactory.makeIcon(IconTypes.colorImage, R.drawable.ic_image));
	}

	@Override
    protected Bitmap doInBackground(File... parameter) {
		final File file = parameter[0];
		final String name = file.getName().toLowerCase();
		Bitmap bitmap = IconGenerate.getCachePreview(file);
		if (bitmap != null) {
			return bitmap;
		}
		if (name.endsWith(".apk")) {
			Apkinfo apkInfo = new Apkinfo(context, file.getAbsolutePath());
			if (!apkInfo.isValid() || apkInfo.icon() == null) {
				return icFactory.makeIcon(Color.GRAY, R.drawable.ic_apk);
			} else {
//			int dc = icf.getDominantColor(apkIcon);
//			return icf.makeIcon(dc, apkIcon, 48, 32, 0);
				bitmap = IconGenerate.drawableToBitmap(apkInfo.icon());
				bitmap = IconGenerate.resizeBitmap(bitmap, 48);
				IconGenerate.saveCachePreview(bitmap, file);
				return bitmap; 
			}
		} else if (name.endsWith(".apks") || name.endsWith(".xapk")) {
			try {
				ZipFile zip = new ZipFile(file);
				Enumeration e = zip.entries();
				int count = 0;
				byte[] buffer = new byte[2048];
				while (e.hasMoreElements()) {
					ZipEntry entry = (ZipEntry) e.nextElement();
					if (entry.getName().toLowerCase().endsWith(".apk") && !entry.getName().contains(File.separator)) {
						final File temp = File.createTempFile("A_TempApkIT", ".tmp", new File(Settings.appCacheDir));
						InputStream is = zip.getInputStream(entry);
						FileOutputStream fos = new FileOutputStream(temp);
						try {
							while ((count = is.read(buffer)) != -1)
								fos.write(buffer, 0, count);
						} finally {
							is.close();
							fos.close();
						}
						Apkinfo apkInfo = new Apkinfo(context, temp.getAbsolutePath());
						if (!apkInfo.isValid() || apkInfo.icon() == null) {
							temp.delete();
							continue;
						} else {
							// bitmap = icFactory.makeIcon(IconTypes.colorArchive, apkInfo.icon(), 48, 32, 0);
							bitmap = IconGenerate.drawableToBitmap(apkInfo.icon());
							bitmap = IconGenerate.resizeBitmap(bitmap, 48);
							IconGenerate.saveCachePreview(bitmap, file);
							temp.delete();
							return bitmap; 
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			try {
				bitmap = IconGenerate.resizeBitmap(file, 48);
				IconGenerate.saveCachePreview(bitmap, file);
				return bitmap;
			} catch (Exception e) {
				return icFactory.makeIcon(Color.GRAY, R.drawable.ic_image);
			}
		}
		int[] types = IconTypes.getType(name);
		return icFactory.makeIcon(types[1], types[0]);
    }

	@Override
    protected void onPostExecute(Bitmap result) {
		if (!icon.getTag().toString().equals(icTag))
			return;
		if (result != null)
			icon.setImageBitmap(result);
    }
}
